import Opt from '@/components/dt/info/Opt.js'
import toolMgr from '@/biz/base/tool.js'
import ruleOpt from '@/biz/option/rule.js'

class SubmitOpt extends Opt {

  constructor(info) {
    super(info)
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.top)
    if (this.info.bonusType == 'SPECIAL') {
      this.opts.push(...this.special)
    }
    this.opts.push(...this.bottom)
  }

  createAll() {
    let preOpt = {
      title: '预发比例',
      key: 'preRatio',
      visible: false,
      group: [{
        key: 'preRatio',
        placeholder: '请输入预发比例',
        input: {
          type: 'number',
          max: 100,
          min: 0
        }
      }, {
        placeholder: '%',
        text: {}
      }],
      rules: [
        ruleOpt.required('请输入预发比例', 'number')
      ]
    }
    
    let tmplOpt = {
      title: '发放模板比例(回佣-年底-交房)',
      key: 'settlementRatio',
      tip: '请注意发放比例之和需为100%',
      visible: false,
      group: [{
        key: 'settlementRatio',
        placeholder: '回佣比例',
        col: 6,
        input: {
          type: 'number',
          max: 100,
          min: 0
        }
      }, {
        placeholder: '% - ',
        text: {},
        col: 2,
      }, {
        key: 'yearEndRatio',
        placeholder: '年底比例',
        col: 6,
        input: {
          type: 'number',
          max: 100,
          min: 0
        }
      }, {
        placeholder: '% - ',
        text: {},
        col: 2,
      }, {
        key: 'handoverRatio',
        placeholder: '交房比例',
        col: 6,
        input: {
          type: 'number',
          max: 100,
          min: 0
        }
      }, {
        placeholder: '%',
        text: {},
        col: 2,
      }],
      rules: [
        ruleOpt.required('请输入发放模板比例', 'number'),
        ruleOpt.check('请输入发放模板比例, 支持自然数', (value) => {
          let reg = /^\d+$/
          return !reg.test(value) || !reg.test(this.info.yearEndRatio) || !reg.test(this.info.handoverRatio)
        }),
        ruleOpt.check('发放模板比例之和须为100', (value) => {
          return (this.info.yearEndRatio + this.info.handoverRatio + value) != 100
        })
      ]
    }
    
    this.special = [{
      title: '本次发放类型',
      key: 'specialGrantType',
      placeholder: '请选择本次发放类型',
      select: {
        key: 'specialGrantType'
      },
      rules: [
        ruleOpt.required('请选择本次发放类型')
      ],
      change: () => {
        let type = this.info.specialGrantType
        preOpt.visible = type == 'PRE' ? true : false
        tmplOpt.visible = type == 'PRE' || type == 'BACK_COMM' ? true : false
        this.info.preRatio = null
        this.info.settlementRatio = null
        this.info.yearEndRatio = null
        this.info.handoverRatio = null
      }
    },
    preOpt,
    tmplOpt,
    {
      title: 'oa文电编号',
      key: 'oaCode',
      placeholder: '请输入oa文电编号',
      input: {},
      rules: [
        ruleOpt.max(20)
      ]
    }]

    this.top = [{
        title: '奖金单月份',
        key: 'bonusMonth',
        text: {}
      },
      {
        title: '奖金总额',
        key: 'bonusAmount',
        text: {
          type: 'number',
          unit: '元'
        }
      }
    ]

    this.bottom = [{
        title: '附件上传',
        key: 'accessoryBOList',
        upload: {
          title: '上传文件',
          api: toolMgr.unloadFile(),
          multiple: true,
          preview: (file) => {
            toolMgr.downloadFile(file.url, file.name)
          },
        },
        data: {
          to: (v) => {
            let accessoryBOList = v.map(item => {
              return {
                accessoryName: item.name,
                accessoryUrl: item.response ? item.response.data.imageURL : item.url
              }
            })
            return {
              accessoryBOList
            }
          }
        },
        rules: this.info.bonusType == 'SPECIAL' ? [
          ruleOpt.required('请上传附件', 'array')
        ] : null
      },
      {
        title: '备注',
        key: 'remark',
        input: {
          type: 'textarea',
          autosize: {
            minRows: 2
          }
        },
        rules: this.info.bonusType == 'SPECIAL' ? [
          ruleOpt.required('请输入备注')
        ] : null
      }
    ]
  }
}

export default SubmitOpt
